When accessing a MySQL back-end database
using a Python front-end client, your Python application needs to know
where the data required to complete your query is stored. A cursor indicates where this data
is positioned on the database. Over the next few minutes,
you'll explore the concept of cursors, their key characteristics,
and learn how they work. Let's look at an example of cursors
from Little Lemon's database. Little Lemon need to retrieve
the guests booking details, they can carry out this task with
a sequel select query using Python. However, the Python front-end client
needs to know where the data is stored within the back-end MySQL database. The MySQL database can use a cursor
object to point to the records that Little Lemon needs. This cursor helps the Python
client locate the required data. This example offers a good understanding
of what database engineers mean by the term cursors. A cursor is a pointer that directs
the Python client to the results of your SQL query within the MySQL database. The cursor indicates the location
of the queried data by identifying specific roles or records. You can use a cursor to read,
retrieve and move through individual records
within the results of your query. Cursors have several
key characteristics or features that are particularly
useful to database engineers. For example, cursors are read-only, so you can't update the data that
they are associated with. The results can't be modified,
they're preserved by the cursor. Cursors are also non-scrollable. They fetch records in order, which helps to keep track of your position
when processing individual records. You can't skip or jump between records or
fetch them in reverse order. And cursors are also a sensitive,
this means that they point to the original data within the MySQL
database instead of a copy. This is faster than using insensitive
cursors, which take longer to return results because they can only
point to a copy of the data. So how can you use a cursor
in a MySQL database query? The first step is to declare the cursor. Use the declare statement and
assign your cursor a custom name, followed by the cursor keyword,
then use the FOR keyword and a relevant SQL select statement to
determine the purpose of the cursor. Next, you need to open the cursor,
type the open command and call your cursors name to
establish the results search. Now the cursor is pointing to the set
of results from your select statement, the next step is to fetch or
retrieve the results of your statement. Type the FETCH command and the name of
your cursor, then type the INTO keyword, followed by the name of the location
the results need to be transferred to. For example, the results can be
transferred to a local variable to be used in your Python application. The final step is to close the cursor,
type the close command, followed by the cursor name. Closing a cursor is always good practice
to release the memory associated with it. As you learned earlier,
a cursor is non-scrollable, it works through the result set in order. So once it reaches the last result,
it no longer needs to remain open, so close the cursor to free
up the memory it uses. Now that you're familiar with how cursors
work, let's return to Little Lemon's query. Little Lemon can use a cursor to retrieve
the booking data for their guest. First, they declare a new cursor
called guest_booking_details, this is followed by a SQL select
statement that targets the guests data from the Little Lemon MySQL database,
they then open the cursor. Next, they fetch the data and store it in a variable within their
Python client called booking data. You should now be able to explain
what cursors are in a MySQL database, describe their key characteristics
and explain how they work. You'll explore cursors in much more detail
as you progress through this course. So this is a great start to
your MySQL Python journey.